<?php
/**
 * Flash Messenger - with namespacing
 */
class ImpSoft_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_FlashMessenger {

	protected $_namespace = 'error';
    /**
     * addMessage() - Add a message to flash message in specified namespace (defaults to "default")
     *
     * @param  string $message
     * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
     */
    public function addMessage($message, $namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if (self::$_messageAdded === false) {
            self::$_session->setExpirationHops(1, null, true);
        }
        if (!is_array(self::$_session->{$namespace})) {
            self::$_session->{$namespace} = array();
        }
        self::$_session->{$namespace}[] = $message;
        return $this;

    }

    /**
     * hasMessages() - Wether a specific namespace has messages
     *
     * @return boolean
     */
    public function hasMessages($namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        return isset(self::$_messages[$namespace]);
    
    }

    /**
     * getMessages() - Get messages from a specific namespace
     *
     * @return array
     */
    public function getMessages($namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if ($this->hasMessages($namespace)) {
            return self::$_messages[$namespace];
        }
        return array();
    
    }

    /**
     * Clear all messages from the previous request & current namespace
     *
     * @return boolean True if messages were cleared, false if none existed
     */
    public function clearMessages($namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if ($this->hasMessages($namespace)) {
            unset(self::$_messages[$namespace]);
            return true;
        }
        return false;
    
    }

    /**
     * hasCurrentMessages() - check to see if messages have been added to current
     * namespace within this request
     *
     * @return boolean
     */
    public function hasCurrentMessages($namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        return isset(self::$_session->{$namespace});
	
    }

    /**
     * getCurrentMessages() - get messages that have been added to the current
     * namespace within this request
     *
     * @return array
     */
    public function getCurrentMessages() {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if ($this->hasCurrentMessages($namespace)) {
            return self::$_session->{$namespace};
        }
        return array();

    }

    /**
     * clear messages from the current request & current namespace
     *
     * @return boolean
     */
    public function clearCurrentMessages() {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if ($this->hasCurrentMessages($namespace)) {
            unset(self::$_session->{$namespace});
            return true;
        }
        return false;

    }

    /**
     * count() - Complete the countable interface
     *
     * @return int
     */
    public function count($namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        if ($this->hasMessages($namespace)) {
            return count($this->getMessages($namespace));
        }
        return 0;

    }

    /**
     * Strategy pattern: proxy to addMessage()
     * 
     * @param  string $message 
     * @return void
     */
    public function direct($message, $namespace = null) {
    
    	if (is_null($namespace)) $namespace = $this->_namespace;
        return $this->addMessage($message, $namespace);
	
    }
}
